<template>
  <div class="Home">
    <el-container>
      <el-header>
        <div class="title">
          <span>共享汽车后台管理系统</span>
        </div>
      <el-button type="info" @click="logout">退出</el-button>
      </el-header>
      <el-container>
        <el-aside :width="isCollaspse ? '60px' : '200px'">
            <el-menu
              background-color="#333744"
              text-color="#fff"
              :unique-opened='true'
              :collapse-transition="false"
              :collapse="isCollaspse"
              :router="true"
              :default-active="activePath"
              active-text-color="#ffd04b">
              <!-- 一级菜单 -->
              <el-submenu
                v-for="item in menuList"
                :key="item.id"
                :index="item.id+''"
                >
                <template slot="title">
                  <!-- 一级菜单图标 -->
                  <i :class="iconObj[item.id]"></i>
                  <!-- 一级菜单文本 -->
                  <span>{{item.authName}}</span>
                </template>
                  <el-menu-item
                    :index="'/'+sbuItem.path"
                    v-for="sbuItem in item.children"
                    :key="sbuItem.id"
                    @click="saveNavState('/'+sbuItem.path)"
                  >
                    <template slot="title">
                      <!-- 二级菜单图标 -->
                      <i class="el-icon-menu"></i>
                      <!-- 二级菜单文本 -->
                      <span>{{sbuItem.authName}}</span>
                    </template>
                  </el-menu-item>
              </el-submenu>
            </el-menu>
        </el-aside>
        <el-main>
        <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
export default {
  name: '',
  data () {
    return {
      menuList: [],
      iconObj: {
        125: 'iconfont icon-dingbudaohang-zhangh',
        103: 'iconfont icon-quanxianshezhi',
        101: 'iconfont icon-shangpinguanli',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-SHUJU'
      },
      isCollaspse: false,
      // 记录当前选中的菜单
      activePath: ''
    }
  },
  created () {
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    logout () {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    // 获得菜单
    async getMenuList () {
      // const { data: res } = await this.$http.get('menus')
      // if (res.meta.status === 200) this.menuList = res.data
      this.menuList = [
        {
          authName: '用户管理',
          id: '1',
          path: 'users',
          children: [
            {
              authName: '用户列表',
              id: '2',
              path: 'users',
              children: []
            }
          ]
        },
        {
          authName: '车辆管理',
          id: '3',
          path: 'cars',
          children: [
            {
              authName: '车辆列表',
              id: '4',
              path: 'cars',
              children: []
            },
            {
              authName: '车辆数据统计',
              id: '5',
              path: 'carsEcharts',
              children: []
            }
          ]
        },
        {
          authName: '优惠卷管理',
          id: '6',
          path: 'coupons',
          children: [
            {
              authName: '优惠卷列表',
              id: '7',
              path: 'coupons',
              children: []
            },
            {
              authName: '优惠卷数据统计',
              id: '8',
              path: 'couponsEcharts',
              children: []
            }
          ]
        }
      ]
    },
    // 点击记录当前选中的菜单
    saveNavState (activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    }
  }
}
</script>

<style lang="scss" scoped>
.Home{
  width: 100%;
  height: 100%;
  .el-header{
    display: flex;
    justify-content: space-between;
    align-items: center;
    background: #373f41;
    .title{
      span{
        display: inline-block;
        line-height: 54px;
        font-size: 20px;
        color: #fff;
        font-weight: bold;
      }
    }
  }
  .el-container{
    height: 100%;
    .el-aside{
      background: #333744;
      .iconfont{
        margin-right: 10px;
      }
      .el-menu{
        border-right: none;
      }
      .toggle-collapse{
        height: 30px;
        text-align: center;
        background: rgb(128, 126, 126);
      }
    }
  }
}
</style>
